//build：20240314
//Button group control
import utils from "./uiUtils.js"
import base from "./uiBase.js"
let buttons = {}

buttons.build = function (id, parent) {
    let temp = utils.validateBuild(buttons.all, id, parent, 'buttons')
    let my = {type: 'buttons'}
    my.obj = new utils.GG.NativeBtnmatrix({ uid: id }, temp)
    my.id = id
    /**
     * The data for the button group must be in array format. Example: This represents three rows of buttons, for a total of 12 buttons.
     * ["1", "2", "3", "0", "\n",
     * "4", "5", "6", "Cancel", "\n",
     *  "7", "8", "9", "Confirm", ""]
     * @param {array} d This field is not required. If it is not filled in or is not of type object, it will retrieve data.
     */
    my.data = function (d) {
        if (utils.validateObject(d)) {
            this.obj.lvBtnmatrixSetMap(d)
        } else {
            return this.obj.lvBtnmatrixGetMap()
        }
    }
    /**
     * Clicking any button in the button group retrieves the ID and text of the clicked button using selectedData.
     * Example return: {id:11,text:'Cancel'}
     */
    my.clickedButton = function () {
        let id = this.obj.lvBtnmatrixGetSelectedBtn();
        if (id == 0xFFFF) {
            // Clicking the button group boundary will result in an illegal value of 0xFFFF, and return an empty string.
            return { id: null, text: null }
        }
        let txt = this.obj.lvBtnmatrixGetBtnText(id);
        return { id: id, text: txt }
    }
    /**
     * You can set the state of a specific button in a button group, such as selecting or disabling it.
     * @param {number} id The button index starts from 0 and proceeds from left to right and top to bottom; it is also the ID returned by the clickedButton button.
     * @param {number} state Refer to dxui.Utils.BUTTONS_STATE
     */
    my.setState = function (id, state) {
        this.obj.lvBtnmatrixSetBtnCtrl(id, state)
    }
    /**
     * Clear the configured state of a specific button in a button group.
     * @param {number} id The button index starts from 0 and proceeds from left to right and top to bottom; it is also the ID returned by the clickedButton button.
     * @param {number} state Refer to dxui.Utils.BUTTONS_STATE
     */
    my.clearState = function (id, state) {
        this.obj.lvBtnmatrixClearBtnCtrl(id, state)
    }
    /**
     * You can set the status of all buttons in a button group, such as selected or disabled.
     * @param {number} state Refer to dxui.Utils.BUTTONS_STATE
     */
    my.setAllState = function (state) {
        this.obj.lvBtnmatrixSetBtnCtrlAll(state)
    }
    /**
     * Clear the configured states of all buttons in the button group.
     * @param {number} state Refer to dxui.Utils.BUTTONS_STATE
     */
    my.clearAllState = function (state) {
        this.obj.lvBtnmatrixClearBtnCtrlAll(state)
    }
    /**
     * Set the width of a button with a specific ID to occupy a certain number of squares.
     * @param {number} id Button numbers start from 0.
     * @param {number} width Width spans the number of grids
     */
    my.setBtnWidth = function (id, width) {
        this.obj.lvBtnmatrixSetBtnWidth(id, width)
    }
    /**
     * Set a button icon with a specific ID.
     * @param {number} id Button numbers start from 0.
     * @param {string} src Icon file path
     */
    my.setBtnIcon = function (id, src) {
        this.obj.addEventCb((e) => {
            // Get the drawing control object
            let dsc = e.lvEventGetDrawPartDsc()
            // If we are drawing the id-th button
            if (dsc.type == utils.ENUM.LV_BTNMATRIX_DRAW_PART_BTN && dsc.id == id) {
                // Get image information
                let header = utils.GG.NativeDraw.lvImgDecoderGetInfo(src)
                // Define a region and center it. Note: When converting a size to an area, subtract 1; when converting an area to a size, add 1.
                let x1 = dsc.draw_area.x1 + (dsc.draw_area.x2 - dsc.draw_area.x1 + 1 - header.w) / 2;
                let y1 = dsc.draw_area.y1 + (dsc.draw_area.y2 - dsc.draw_area.y1 + 1 - header.h) / 2;
                let x2 = x1 + header.w - 1;
                let y2 = y1 + header.h - 1;
                let area = utils.GG.NativeArea.lvAreaSet(x1, y1, x2, y2)
                // Drawing image information
                let img_draw_dsc = utils.GG.NativeDraw.lvDrawImgDscInit()
                // Drawing pictures
                utils.GG.NativeDraw.lvDrawImg(dsc.dsc, img_draw_dsc, area, src)
            }
        }, utils.ENUM.LV_EVENT_DRAW_PART_END)
    }
    let comp = Object.assign(my, base);
    utils.setParent(this.all,comp,parent)
    return comp;
}
export default buttons;